<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
<!-- deployment.qdoc -->
  <title>Deploying an Application on Android | Qt 5.14</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="index.html">Qt 5.14</a></td><td >Deploying an Application on Android</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right">Qt 5.14.2 Reference Documentation</td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<p class="naviNextPrevious headerNavi">
</p><p/>
<div class="sidebar">
<div class="toc">
<h3><a name="toc">Contents</a></h3>
<ul>
<li class="level1"><a href="#the-application-bundle">The Application Bundle</a></li>
<li class="level1"><a href="#package-template">Package Template</a></li>
<li class="level2"><a href="#androidmanifest-xml">AndroidManifest.xml</a></li>
<li class="level2"><a href="#java-code">Java Code</a></li>
<li class="level2"><a href="#resources">Resources</a></li>
<li class="level2"><a href="#libraries">Libraries</a></li>
<li class="level1"><a href="#building-the-android-application-package">Building the Android Application Package</a></li>
<li class="level1"><a href="#androiddeployqt">androiddeployqt</a></li>
<li class="level2"><a href="#required-steps-before-running-androiddeployqt">Required Steps Before Running androiddeployqt</a></li>
<li class="level2"><a href="#command-line-arguments">Command Line Arguments</a></li>
<li class="level1"><a href="#dependencies-detection">Dependencies Detection</a></li>
<li class="level1"><a href="#android-specific-qmake-variables">Android-specific qmake Variables</a></li>
<li class="level1"><a href="#deployment-in-qt-creator">Deployment in Qt Creator</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">Deploying an Application on Android</h1>
<span class="subtitle"></span>
<!-- $$$deployment-android.html-description -->
<div class="descr"> <a name="details"></a>
<p>This article gives a technical description of the steps required to take any given Qt application and deploy it to an Android device (or market place).</p>
<p>It is recommended that you use Qt Creator or the Makefile created by qmake to create the application bundle. The following information will give you a technical insight into the structure of the resulting Android application which is not normally required just to write an application.</p>
<p>All the steps described here are handled automatically by the build script and the <a href="deployment-android.html#androiddeployqt">androiddeployqt deployment tool</a>, which are run by Qt Creator for you.</p>
<p><b>Note: </b>If you prefer building Android packages from the command line, you may use the &quot;aab&quot; or &quot;apk&quot; build targets in the Makefile directly:</p><pre class="cpp">

  <span class="operator">%</span> make aab

</pre>
<p>or</p>
<pre class="cpp">

  <span class="operator">%</span> make apk

</pre>
<a name="the-application-bundle"></a>
<h2 id="the-application-bundle">The Application Bundle</h2>
<p>Applications on Android can be packaged in two ways: Either as Application Package (APK) or Android App Bundle (AAB). Both are ZIP files which follow a predefined directory structure. The difference between the two is that APK files can be downloaded to and executed on a device. AAB, on the other hand, is intended to be interpreted by the Google Play store and is used to generate APK files.</p>
<p>For testing the application locally, the APK format is the most appropriate, as this can be uploaded directly to the device and run. For distribution to the Google Play store, it is recommended that you use AAB instead, which has a similar layout. The added convenience of AAB is that you can include all target ABIs in the same bundle without increasing the size of the actual package downloaded by your users. When using AAB, the Google Play store generates optimized APK packages for the devices issuing the download request and automatically sign them with your publisher key.</p>
<p>Read <a href="https://developer.android.com/guide/app-bundle">the Android documentation</a> if you want to know more about the AAB format.</p>
<p>In either case, the files must be copied into a special directory structure first, before bundling them in a single ZIP file</p>
<p>This contains one or more binary <code>.so</code> files with the code for your application, as well as any dependencies, such as Qt's libraries and plugins. In addition, it includes <code>.jar</code> files containing compiled Java code, assets, resources, and some <code>.xml</code> files that are used to describe the contents of the bundle.</p>
<a name="package-template"></a>
<h2 id="package-template">Package Template</h2>
<p>A template for the other sources of an APK package is contained in <code>$QTDIR/src/android/templates</code>. The first step of making a package manually is to copy these files into an empty directory. In this guide, we'll refer to this build directory as <code>$BUILD_TARGET</code>.</p>
<p>We also need to make sure the application binary is copied into the package. This can be achieved by using the following command after running qmake on your application's project file:</p>
<pre class="cpp">

  make install INSTALL_ROOT<span class="operator">=</span>$BUILD_TARGET

</pre>
<p>It will copy the application binary and any other installation requirements into the packaging directory.</p>
<p>The packaging directory will now consist of the following parts:</p>
<a name="androidmanifest-xml"></a>
<h3 id="androidmanifest-xml">AndroidManifest.xml</h3>
<p>The <code>AndroidManifest.xml</code> file gives detailed meta-information about your application. This information is used for several things. It is used by the target device to decide which features to enable, the default orientation of the application, and so on. In addition, it's used by the market place for information on the version code, device support, package name, and lots more.</p>
<p>For more information about general capabilities of and requirements for the <code>AndroidManifest.xml</code> file, please refer to the <a href="http://developer.android.com/guide/topics/manifest/manifest-intro.html">Android documentation on this topic</a>.</p>
<p>The default manifest contains some special parameters used by Qt to set up the application for running. When you are creating your own Android manifest, you must make sure that it contains these parameters. The <code>androiddeployqt</code> tool replaces content in the template with the correct values.</p>
<a name="java-code"></a>
<h3 id="java-code">Java Code</h3>
<p>Under <code>$BUILD_TARGET/src</code> are the files comprising the Java code of the Android application. The regular Android application launcher is a Java process, so Qt applications have a Java-based entry point. The code in here will load the required Qt libraries, based on the meta-information given in other files in the template.</p>
<p>After loading the libraries, the Java code will call into the application's native <code>main()</code> function on a new thread and the application will launch. At this point, the Java code in the template is used to delegate events from Android into Qt.</p>
<p>One thing to note about the files in this directory is that they can contain code specific to certain Android versions. Based on the minimum required Android API level of your application it might be necessary to remove some of this code. This is done automatically by <code>androiddeployqt</code> and Qt Creator during packaging.</p>
<p>For example, lets say the code contains the following:</p>
<pre class="cpp">

  <span class="comment">//@ANDROID-21</span>
      @Override
      <span class="keyword">public</span> <span class="type">void</span> onActivityReenter(<span class="type">int</span> resultCode<span class="operator">,</span> Intent data)
      {
          <span class="comment">// Do something</span>
          <span class="keyword">return</span> super<span class="operator">.</span>onActivityReenter(resultCode<span class="operator">,</span> data);
      }
  <span class="comment">//@ANDROID-21</span>

</pre>
<p>If your minimum Android API level is 20 or lower, the code is removed before building, since it's not a supported API on Android API level 20. However, if your minimum API level is 21 or higher, it is left in.</p>
<a name="resources"></a>
<h3 id="resources">Resources</h3>
<p>Under the <code>res/</code> folder in the <code>$BUILD_TARGET</code> are Android resources that can be accessed from the <code>AndroidManifest.xml</code> and Java code of your application. A typical example of resources which should be placed here are the icon files used by the application launcher to represent your application.</p>
<p>In Qt, some translations used for the Ministro service and some files with meta-information are in the default resources of the application.</p>
<a name="res-values-libs-xml"></a>
<h4 id="res-values-libs-xml">res/values/libs.xml</h4>
<p>One of the files containing meta information about the deployment of the application is <code>libs.xml</code>. It consists of the following values:</p>
<ul>
<li><code>qt_sources</code>: The URL of one or more Ministro repositories that contain the necessary Qt libraries. This is used when the Ministro deployment mechanism is active. Read the <a href="http://necessitas.kde.org/necessitas/ministro.php">Ministro documentation</a> for more information about such repositories.</li>
<li><code>bundled_libs</code>: Libraries in the package's library folder which should be loaded on start-up. Library names should be specified without the <code>lib</code> prefix and <code>.so</code> suffix.</li>
<li><code>qt_libs</code>: Qt libraries which should be loaded on start-up. When bundled deployment is used, these are expected to be found inside the <code>APK</code>'s library folder. When Ministro deployment is in use, they are requested from the Ministro service on the device. And when debugging deployment is in use, they are loaded from the <code>/data/local/tmp/qt</code> directory on the target device.</li>
</ul>
<a name="res-values-strings-xml"></a>
<h4 id="res-values-strings-xml">res/values/strings.xml</h4>
<p>The <code>strings.xml</code> file contains some strings used by the <code>AndroidManifest.xml</code> and by the deployment mechanisms, as well as some strings used when loading the Ministro service.</p>
<p>In particular, the application name and the name of the application binary can be specified here. There are also strings that contain additional libraries that should be loaded and <code>JAR</code> files which should be included in the class path. The latter is only used for deployment with Ministro or debug deployment.</p>
<a name="libraries"></a>
<h3 id="libraries">Libraries</h3>
<p>Under <code>libs</code> in the package directory, it's possible to place libraries that should be included in the application bundle. <code>JAR</code> libraries should be placed directly under <code>libs/</code>, while shared libraries should be put in a subdirectory suitably named after the target ABI of the libraries.</p>
<a name="building-the-android-application-package"></a>
<h2 id="building-the-android-application-package">Building the Android Application Package</h2>
<p>The project can be built using the gradle tool. If an APK intended for release is built, then it should be signed and aligned using <code>jarsigner</code> and <code>zipalign</code>.</p>
<a name="androiddeployqt"></a>
<h2 id="androiddeployqt">androiddeployqt</h2>
<p>Building an application package is complex, so Qt comes with a tool which handles the work for you. The steps described in this document so far are handled automatically by the tool.</p>
<p>In addition, there are Makefile build targets for building the package from the command line. There is also automated support in Qt Creator for generating both <code>APK</code> and <code>AAB</code> packages.</p>
<a name="required-steps-before-running-androiddeployqt"></a>
<h3 id="required-steps-before-running-androiddeployqt">Required Steps Before Running androiddeployqt</h3>
<p>Before running the tool manually, you need to run <code>qmake</code> and <code>make</code> on your project. Running <code>qmake</code> creates the <code>Makefile</code>, and it will also generate a <code>JSON</code> file containing important settings used by <code>androiddeployqt</code>.</p>
<p>You should then install the application binary (and any other requirements) into the library folder of the <code>bundle</code>. If <code>$BUILD_TARGET</code> is your build directory (the first time you do this, the directory should be empty at this point), then you can install the binary with the following command:</p>
<pre class="cpp">

  <span class="operator">%</span> make install INSTALL_ROOT<span class="operator">=</span>$BUILD_TARGET

</pre>
<a name="command-line-arguments"></a>
<h3 id="command-line-arguments">Command Line Arguments</h3>
<p>The only required command line argument when running the tool is <code>--output</code>. This should be set to <code>$BUILD_TARGET</code>, that is: the build directory where you installed your application binary.</p>
<p>Other command line arguments are optional but useful. Here's a quick overview. More information is available by passing the <code>--help</code> argument to androiddeployqt.</p>
<ul>
<li><code>--aab</code>: Generate an Android Application Bundle, rather than an APK. Note that this invalidates some of the other arguments, such as --install.</li>
<li><code>--input &lt;file name&gt;</code>: This allows you to specify the <code>JSON</code> file generated by <code>qmake</code>. By default, <code>androiddeployqt</code> will try to guess the file name based on the current working directory.</li>
<li><code>--deployment &lt;mechanism&gt;</code>: Specify this to pick a different deployment mechanism than the default.</li>
<li><code>--install</code>: Specify this to install the finished package on the target device or emulator. Note that if a previous version of the package is already installed, it will be uninstalled first, removing any data it might have stored locally.</li>
<li><code>--device &lt;ID&gt;</code>: Specify the ID of the target device or emulator as reported by the <code>adb</code> tool. If an ID is specified, it will be passed to all calls to <code>adb</code>. If it is unspecified, no particular device or emulator will be requested by <code>adb</code>, causing it to pick a default instead.</li>
<li><code>--android-platform &lt;platform&gt;</code>: The SDK platform used for building the Java code of the application. By default, the latest available platform is used.</li>
<li><code>--release</code>: Specify this to create a release package instead of a debug package. With no other arguments, release packages are unsigned and cannot be installed to any device before they have been signed by a private key.</li>
<li><code>--sign &lt;url&gt; &lt;alias&gt;</code>: Sign the resulting package. Specifying this also implies <code>--release</code>. The URL of the keystore file and the alias of the key have to be specified. In addition, there are a number of options that can be specified which are passed through to the <code>jarsigner</code> tool. Pass <code>--help</code> to <code>androiddeployqt</code> for more information about these.</li>
<li><code>--jdk &lt;path&gt;</code>: Specify the path to the Java Development Kit. This is only required for signing packages, as it is only used for finding the <code>jarsigner</code> tool. If it is unspecified, then <code>androiddeployqt</code> will attempt to detect <code>jarsigner</code>, either using the <code>JAVA_HOME</code> environment variable, or on the <code>PATH</code>.</li>
<li><code>--verbose</code>: Specify this to output more information about what <code>androiddeployqt</code> is doing.</li>
</ul>
<a name="dependencies-detection"></a>
<h2 id="dependencies-detection">Dependencies Detection</h2>
<p>Qt comes with a number of plugins which are loaded at run-time when they are needed. These can handle anything from connecting to SQL databases to loading specific image formats. Detecting plugin dependencies is impossible as the plugins are loaded at run-time, but androiddeployqt tries to guess such dependencies based on the Qt dependencies of your application. If the plugin has any Qt dependencies which are not also dependencies of your application, it will not be included by default. For instance, in order to ensure that the SVG image format plugin is included, you will need to add <code>QT += svg</code> to your <code>.pro</code> file so that the <a href="../qtsvg/qtsvg-index.html">Qt SVG</a> module becomes a dependency of your application.</p>
<p>If you are wondering why a particular plugin is not included automatically, you can run androiddeployqt with the <code>--verbose</code> option to get the list of missing dependencies for each excluded plugin. You can achieve the same in Qt Creator by ticking the <b>Verbose output</b> check box in the <b>Deployment configurations</b>. This is located in the <b>Run</b> tab of your <b>Projects</b> settings.</p>
<p>It's also possible to manually specify the dependencies of your application. See the documentation for the <code>ANDROID_DEPLOYMENT_DEPENDENCIES</code> qmake variable below.</p>
<a name="android-specific-qmake-variables"></a>
<h2 id="android-specific-qmake-variables">Android-specific qmake Variables</h2>
<p>Unless the project has special requirements such as third party libraries, it should be possible to run <code>androiddeployqt</code> on it with no modifications and get a working Qt for Android application as a result.</p>
<p>However, there are a set of <code>qmake</code> variables that can be used to tailor your package. At some point during development, you will most likely want to look into these variables, as they will e.g&#x2e; allow you to set the name of your application as it appears in the application menu on devices.</p>
<p>Here is a list of some variables that are particularly interesting when making Android applications:</p>
<ul>
<li><code>ANDROID_DEPLOYMENT_DEPENDENCIES</code>: By default, <code>androiddeployqt</code> will detect the dependencies of your application. But since run-time usage of plugins cannot be detected, there could be false positives, as your application will depend on any plugins that are <i>potential</i> dependencies. If you want to minimize the size of your <code>APK</code>, it's possible to override the automatic detection using the <code>ANDROID_DEPLOYMENT_DEPENDENCIES</code> variable. This should contain a list of all Qt files which need to be included, with paths relative to the Qt install root. Note that only the Qt files specified here will be included. Failing to include the correct files can result in crashes. It's also important to make sure the files are listed in the correct loading order. This variable provides a way to override the automatic detection entirely, so if a library is listed before its dependencies, it will fail to load on some devices.</li>
<li><code>ANDROID_PACKAGE_SOURCE_DIR</code>: This variable can be used to specify a directory where additions and modifications can be made to the default Android package template. The <code>androiddeployqt</code> tool will copy the application template from Qt into the build directory, and then it will copy the contents of the <code>ANDROID_PACKAGE_SOURCE_DIR</code> on top of this, overwriting any existing files. The update step where parts of the source files are modified automatically to reflect your other settings is then run on the resulting merged package. If you, for instance, want to make a custom <code>AndroidManifest.xml</code> for your application, then place this directly into the folder specified in this variable. You can also add custom Java files in <code>ANDROID_PACKAGE_SOURCE_DIR/src</code>.<p><b>Note: </b>When adding custom versions of the build files (like strings.xml, libs.xml, AndroidManifest.xml, etc.) to your project, make sure you copy them from the package template, which is located in <code>$QT/src/android/java</code>. You should never copy any files from the build directory, as these files have been altered to match the current build settings.</p></li>
<li><code>ANDROID_EXTRA_LIBS</code>: A list of external libraries that will be copied into your application's library folder and loaded on start-up. This can be used, for instance, to enable OpenSSL in your application. Simply set the paths to the required <code>libssl.so</code> and <code>libcrypto.so</code> libraries here and OpenSSL should be enabled automatically.</li>
<li><code>ANDROID_EXTRA_PLUGINS</code>: This variable can be used to specify different resources that your project has to bundle but cannot be delivered through the assets system, such as qml plugins. When using this variable, <code>androiddeployqt</code> will make sure everything is packaged and deployed properly.</li>
</ul>
<a name="deployment-in-qt-creator"></a>
<h2 id="deployment-in-qt-creator">Deployment in Qt Creator</h2>
<p>Qt Creator will run the <code>androiddeployqt</code> tool for you, and provides easy and intuitive user interfaces to specify many of the options. For more information, see <a href="http://doc.qt.io/qtcreator/creator-deploying-android.html">the Qt Creator documentation</a>.</p>
</div>
<!-- @@@deployment-android.html -->
<p class="naviNextPrevious footerNavi">
</p>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2020 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
